#include <wx/wx.h>
#include <wx/filefn.h>
#include <UnitTest++.h>

#include "pm/project.h"
#include "pm/workrequest.h"
#include "pm/task.h"
#include "pm/taskhistory.h"
#include "pm/cost.h"
#include "pm/manager.h"

struct TaskData {
	wxString   name;
	double     unitCost;
	double     quantity;
	wxDateTime endDate;
	wxString   comment;
	db::ID     idProject;
	db::ID     idTag;
};

static const wxDateTime today = wxDateTime::Today();

static const TaskData taskDatas[] = {
	{ wxT("Management"),     40, 50, today + wxDateSpan(0, 3), wxT(""), 1, db::Object::undefined },
	{ wxT("Specifications"), 40, 50, today + wxDateSpan(0, 1), wxT(""), 1, db::Object::undefined },
	{ wxT("Spec review"),     4, 50, today + wxDateSpan(0, 1), wxT(""), 1, db::Object::undefined },
	{ wxT("Design"),         32, 50, today + wxDateSpan(0, 1), wxT(""), 1, db::Object::undefined },
	{ wxT("Design review"),   4, 50, today + wxDateSpan(0, 1), wxT(""), 1, db::Object::undefined },
	{ wxT("Coding"),         40, 50, today + wxDateSpan(0, 2), wxT(""), 1, db::Object::undefined },
	{ wxT("Coding review"),   4, 50, today + wxDateSpan(0, 2), wxT(""), 1, db::Object::undefined },
	{ wxT("Tests"),          40, 50, today + wxDateSpan(0, 3), wxT(""), 1, db::Object::undefined },
	{ wxT("Tests review"),    4, 50, today + wxDateSpan(0, 2), wxT(""), 1, db::Object::undefined },
	{ wxT("End project"),    16, 50, today + wxDateSpan(0, 3), wxT(""), 1, db::Object::undefined }
};

static const size_t taskDataCount = WXSIZEOF(taskDatas);

// Test database creation
TEST( InitDatabase )
{
	if (wxFileExists(wxT("test.pmdb"))) {
		wxRemoveFile(wxT("test.pmdb"));
	}
	
	CHECK( pm::load(wxT("test.pmdb")) );
}

// Test project creation
TEST( CreateProject )
{
	pm::Project project;
	project.name( wxT("MyProject") );
	CHECK( pm::insert(project) );
	CHECK_EQUAL(1, project.id());
	
	pm::WorkRequest wr;
	wr.amount(50000);
	wr.name(wxT("Initial work request"));
	wr.date(wxDateTime::Today());
	wr.id_project(project.id());
	CHECK(pm::insert(wr));
	
	for (size_t i = 0; i < taskDataCount; ++i) {
		pm::Task task;
		task.name(taskDatas[i].name);
		task.unit_cost(taskDatas[i].unitCost);
		task.quantity(taskDatas[i].quantity);
		task.comment(taskDatas[i].comment);
		task.end_date(taskDatas[i].endDate);
		task.id_project(taskDatas[i].idProject);
		CHECK(pm::insert(task));
		
		pm::CreateInitialTaskHistory critInsertNewtask(task);
		CHECK(pm::query(critInsertNewtask));
	}
}

// run all tests
int main(int argc, char **argv)
{
	wxInitializer init;
	return UnitTest::RunAllTests();
}

